
/**
* @module index
* @author: Hjava
* @description: IndexedDB操作入口
* @since: 2018-07-23 18:37:25
*/
Object.defineProperty(exports, "__esModule", { value: true });
var web_db_1 = __webpack_require__(/*! ./web-db */ "./src/extra/dao/web-db.ts");
var promise_1 = __webpack_require__(/*! _util/promise */ "./src/core/utils/promise.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/extra/utils/log.ts");
var size = 50 * 1024 * 1024;
var DBCfg = {
    session: {
        fields: ['uid', 'appId', 'channelId', 'sid', 'time', '_isCountUnread', 'unreads', 'type', 'message', 'readTime', 'pubId', 'peerUid', 'svrTime']
    }
};
var dbController = {
    initDB: function (uid) {
        log_1.default.log('dbController::initDB uid:' + uid);
        for (var key in DBCfg) {
            DBCfg[key].db = new web_db_1.default(uid + key, DBCfg[key].fields, size);
            if (DBCfg[key].db && !DBCfg[key].db.setBatch) {
                DBCfg[key].db = null;
            }
        }
    },
    /**
     * 获取数据
     * @param {string} dbname 数据库名字
     * @param {string} key 数据的key值
     * @return {promise} resolve(data), reject(error)
     */
    clearDBs: function () {
        var promiseArr = [];
        for (var key in DBCfg) {
            promiseArr.push(this.clearDB(key));
        }
        return promise_1.default.all(promiseArr);
    },
    clearDBsExclude: function (excludes) {
        var promiseArr = [];
        for (var key in DBCfg) {
            if (excludes.indexOf(key) === -1) {
                promiseArr.push(this.clearDB(key));
            }
        }
        return promise_1.default.all(promiseArr);
    },
    /**
     * 获取数据
     * @param {string} dbname 数据库名字
     * @param {string} key 数据的key值
     * @return {promise} resolve(data), reject(error)
     */
    getData: function (dbname, key) {
        if (!DBCfg[dbname].db) {
            return promise_1.default.resolve({});
        }
        try {
            if (!key) {
                return promise_1.default.reject({});
            }
            return new promise_1.default(function (resolve, reject) {
                if (DBCfg[dbname].db) {
                    DBCfg[dbname].db.get(key, resolve, reject);
                }
                else {
                    return resolve({});
                }
            });
        }
        catch (e) {
            log_1.default.error('dbController::getData error', e);
            return promise_1.default.reject({});
        }
    },
    _handler: function () {
    },
    registerErrHandler: function (handler) {
        this._handler = handler;
    },
    /**
     * 设置数据
     * @param {string} dbname 数据库名字
     * @param {string} key 数据的key值
     * @param {object} data 数据对象
     * @return {promise} resolve(data), reject(error)
     */
    setData: function (dbname, key, data) {
        if (!DBCfg[dbname].db) {
            return promise_1.default.resolve({});
        }
        return new promise_1.default(function (resolve, reject) {
            try {
                if (DBCfg[dbname].db) {
                    DBCfg[dbname].db.set(key, data, resolve, reject);
                }
                else {
                    return resolve({});
                }
            }
            catch (e) {
                log_1.default.error('dbController::setData error', e);
            }
        });
    },
    /**
     * 设置数据
     * @param {string} dbname 数据库名字
     * @param {object} data 数据对象数组
     * @return {promise} resolve(data), reject(error)
     */
    setBatch: function (dbname, dataObj) {
        return new promise_1.default(function (resolve, reject) {
            try {
                if (DBCfg[dbname].db) {
                    DBCfg[dbname].db.setBatch(dataObj, resolve, reject);
                }
                else {
                    return resolve({});
                }
            }
            catch (e) {
                log_1.default.error('dbController::setBatch error', e);
            }
        });
    },
    /**
     * 批量删除数据
     * @param {string} dbname 数据库名字
     * @param {array} keys 数据key值数组
     * @returns {*|Promise} resolve(data), reject(error)
     */
    removeBatch: function (dbname, keys) {
        return new promise_1.default(function (resolve, reject) {
            try {
                if (DBCfg[dbname].db) {
                    DBCfg[dbname].db.removeBatch(keys, resolve, reject);
                }
                else {
                    return resolve({});
                }
            }
            catch (e) {
                log_1.default.error('dbController::removeBatch error', e);
            }
        });
    },
    /**
     * 过滤方法
     * @param {string} dbname 数据库名字
     * @param options 有以下参数
     *  @param {number | undefined} after 开始时间，表示帅选出 跟新时间晚于该时间 的数据
     *  @param {number | undefined} before 结束时间，表示数据 更新时间早于该时间 的数据
     *  @param {number | undefined} limit 数目上限
     *  @param {Function} fn 过滤条件
     * @returns {*}
     */
    filter: function (dbname, options) {
        return new promise_1.default(function (resolve, reject) {
            options.onSuccess = resolve;
            options.onFail = reject;
            if (DBCfg[dbname].db) {
                DBCfg[dbname].db.filter(options);
            }
            else {
                return resolve({});
            }
        });
    },
    /**
     * 移除数据
     * @param {string} dbname 数据库名字
     * @param {string | Array} key 数据的key值
     * @return {promise} resolve(data), reject(error)
     */
    removeData: function (dbname, key) {
        return new promise_1.default(function (resolve, reject) {
            try {
                if (DBCfg[dbname].db) {
                    if (!(key instanceof Array)) {
                        DBCfg[dbname].db.remove(key, resolve, reject);
                    }
                    else {
                        DBCfg[dbname].db.removeBatch(key, resolve, reject);
                    }
                }
                else {
                    return resolve({});
                }
            }
            catch (e) {
                log_1.default.error('dbController::removeData error', e);
            }
        });
    },
    getAll: function (dbname) {
        return new promise_1.default(function (resolve, reject) {
            try {
                if (DBCfg[dbname].db) {
                    DBCfg[dbname].db.all(resolve, reject);
                }
                else {
                    return resolve({});
                }
            }
            catch (e) {
                log_1.default.error('getAll', e);
            }
        });
    },
    /**
     * 清除数据库
     * @param {string} dbname 数据库名字
     * @return {promise} resolve(data), reject(error)
     */
    clearDB: function (dbname) {
        return new promise_1.default(function (resolve, reject) {
            try {
                if (DBCfg[dbname].db) {
                    DBCfg[dbname].db.clear(resolve, reject);
                }
                else {
                    return resolve({});
                }
            }
            catch (e) {
                log_1.default.error('dbController::clearDB error', e);
            }
        });
    }
};
exports.default = dbController;


//# sourceURL=webpack://MTDX/./src/extra/dao/index.ts?